﻿Imports Microsoft.Win32

NotInheritable Class FileShellExtension
    Private Sub New()
    End Sub
    Public Shared Sub Register(fileType As String, shellKeyName As String, menuText As String, menuCommand As String)
        ' create path to registry location
        Dim regPath As String = String.Format("{0}\shell\{1}", fileType, shellKeyName)

        ' add context menu to the registry
        Using key As RegistryKey = Registry.ClassesRoot.CreateSubKey(regPath)
            key.SetValue(Nothing, menuText)
        End Using

        ' add command that is invoked to the registry
        Using key As RegistryKey = Registry.ClassesRoot.CreateSubKey(String.Format("{0}\command", regPath))
            key.SetValue(Nothing, menuCommand)
        End Using
    End Sub

    Public Shared Sub Unregister(fileType As String, shellKeyName As String)
        Debug.Assert(Not String.IsNullOrEmpty(fileType) AndAlso Not String.IsNullOrEmpty(shellKeyName))

        ' path to the registry location
        Dim regPath As String = String.Format("{0}\shell\{1}", fileType, shellKeyName)

        ' remove context menu from the registry
        Registry.ClassesRoot.DeleteSubKeyTree(regPath)
    End Sub
End Class